﻿using Acme.SmartWMS.BasicInfo.SupplierService.DTO;
using Acme.SmartWMS.Common;
using Acme.SmartWMS.DataBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace Acme.SmartWMS.BasicInfo.SupplierService
{
    public class SupplierAppService:ApplicationService,ISupplierAppService
    {
        /// <summary>
        /// 基础资料-供应商管理仓储
        /// </summary>
        private readonly IRepository<Supplier,int>  _supplierRepository;
        /// <summary>
        /// 字典明细表仓储
        /// </summary>
        private readonly IRepository<DictItem,int> _dictItemRepository;

        public  SupplierAppService(IRepository<Supplier, int> supplierRepository, IRepository<DictItem, int> dictItemRepository)
        {
            _supplierRepository = supplierRepository;
            _dictItemRepository = dictItemRepository;
        }
        /// <summary>
        /// 获取供应商管理显示 分页 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="name"></param>
        /// <param name="supplierCode"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetSupplierList(int pageIndex,int pageSize,string? name,string? supplierCode)
        {
            var predicate =  PredicateBuilder.New<Supplier>(true);
            if (!string.IsNullOrEmpty(name))
            {
                predicate=predicate.And(m=>m.Name.Contains(name));
            }
            if (!string.IsNullOrEmpty(supplierCode))
            {
                predicate = predicate.And(m => m.SupplierCode.Contains(supplierCode));
            }
            var dto = await _supplierRepository.GetListAsync(predicate);
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / pageSize);
            result.CurrentPage = pageIndex;
            result.PageSize = pageSize;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            return result;
        }
        /// <summary>
        /// 获取字典表数据，绑定企业属性下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictItem>> GetDictItems(int dictTypeId)
        {
            var dictItem = await _dictItemRepository.GetListAsync(x=>x.DictTypeId==dictTypeId);
            return dictItem;

        }
        /// <summary>
        /// 添加供应商管理
        /// </summary>
        /// <param name="supplierDTO"></param>
        /// <returns></returns>
        public async Task<int> InsertSupplier(SupplierDTO supplierDTO)
        {
            var supplierCode= _supplierRepository.CountAsync(x=>x.SupplierCode == supplierDTO.SupplierCode).Result;
            if (supplierCode > 0)
            {
                return -1;
            }
            var supplier = supplierDTO.MapTo<Supplier>();
            await _supplierRepository.InsertAsync(supplier);
            return 1;
        }
        /// <summary>
        /// 删除供应商管理
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteSupplier(string ids)
        {
            string[] array = ids.Split(',');
            await _supplierRepository.DeleteAsync(x => ids.Contains(x.Id.ToString()));
            return 1;
        }
        /// <summary>
        /// 获取供应商主键id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<SupplierDTO> GetSupplierById(int id)
        {
            var supplier=await _supplierRepository.GetAsync(x=>x.Id==id);
            var supplierDTO=supplier.MapTo<SupplierDTO>();
            return supplierDTO;
        }
        /// <summary>
        /// 更新供应商
        /// </summary>
        /// <param name="supplierDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateSupplier(SupplierDTO  supplierDTO)
        {
            var supplier=_supplierRepository.CountAsync(x=>x.SupplierCode==supplierDTO.SupplierCode).Result;
            if (supplier > 0)
            {
                return -1;
            }
            var suppliers=supplierDTO.MapTo<Supplier>();
            await _supplierRepository.UpdateAsync(suppliers);
            return 1;
        }
    }
}
